Skip to content

fix(durable): dispatch effects from Start, Tick and RunService - #235

Merged
reka18 merged 8 commits into
mainfrom
fix/durable-effect-dispatch
Nov 17, 2026
Merged

reka18 merged 8 commits into
mainfrom
fix/durable-effect-dispatch

Conversation

@reka18

@reka18 reka18 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

What this change does

Domain effects now reach the handler from Start, Tick and RunService, not only Fire, and a timer transition now starts and stops services and actors.

  • Tick and RunService record their step the way Fire does: append under the step lock, then dispatch the effects outside it, before any checkpoint.
  • A failed dispatch fails the Handle before its checkpoint. Recover retries the effect, and fails with ErrEffectDispatch while the handler still does. A handler panic, or a panic or error in the Store's dispatch tracking, returns ErrEffectDispatch too, from Start, a step and Recover. This changes behavior: a Fire after a failed dispatch used to be accepted.
  • Start encodes the baseline, applies the initial entry's effects, then writes the baseline, and applies none if the entry failed. Those effects are at-least-once: a handler failure, a failed baseline write or overlapping Start calls re-apply them. Tests pin this contract.
  • Recover re-sends a Record's unmarked effects only for this version's Records (journal format 2), and for every Fire Record, so an older journal's uncompacted Tick and service steps don't send effects they never sent.
  • A KindedEffect whose payload can't marshal, or whose MarshalJSON panics, no longer fails a step: it's recorded by id and kind, with the error under Meta["crucible.marshalError"], and Recover re-derives its value for dispatch. This relaxes Fire, which failed in this case on main.
  • New durable.EffectInstance(ctx) gives a handler the instance, because effect ids repeat across instances (durable: defects found in the platform-redesign review #214 question 5). This takes the issue's second option, so the dispatched set needs no migration.
  • New state.ServiceRunner.WithResultTransform, an additive change to the frozen state module. durable uses it so a service result reaches Assign in its JSON-decoded form live as well as on replay, so a retried effect can't differ from the first attempt under the same id. A result that encodes but won't decode fails the step and records nothing; a service error stays message-only, so errors.Is and errors.As don't match in onError.
  • A timer transition starts and stops services and actors, live and on replay, as Fire and RunService already did. Without it, a timer that left a service's state left the service in flight, and a checkpoint followed by a settle made every Recover fail with "no in-flight service to settle". Timer-driven work runs under the step's context, and a multi-timer Tick starts every timer's services before its actors. This covers fix(durable): keep timers across recovery and skip idle ticks #236's Tick items and unskips main's two "fix(durable): keep timers across recovery and skip idle ticks #236 fixes it" tests.
  • A step's actor work follows Start and Recover's rule: only a parent Fire that an ended context refused fails the step. A message an actor refuses because the step's context ended is lost, as actor deliveries are across Recover.
  • A parent forwardTo on Fire or a timer transition forwards the step's event instead of nil (on main, Fire forwarded nil).
  • A state's actor isn't spawned once its state was left in the same step, at Fire, Tick, Start and Recover, so the live Handle matches what Recover rebuilds.

Please check these:

  • Behavior change for your ratification (durable: defects found in the platform-redesign review #214 question 3): a Runner with WithEffectHandler on a Store that isn't a DispatchStore is now refused with the new exported ErrNoDispatchStore, instead of silently applying no effects. NewRunner can't return an error, so Start, Recover and the stateless Fire refuse before any Store read, write or handler call. It's a permanent configuration error, so it doesn't match ErrEffectDispatch, which callers retry. MemStore and FileStore both implement DispatchStore. I'd suggest refusing, but it's your call: the alternatives are dispatching at-least-once with no dedupe on such Stores, or keeping the documented silent skip.
  • Behavior change for your ratification: a service result reaches Assign JSON-decoded on the live run too. Before, live got the ServiceFn's Go value and replay got generic JSON. Now both get the generic form (a struct as map[string]any, a number as float64), so live and replay agree. An existing reducer that type-asserts the concrete result stops matching on the live run without an error. The fooddelivery example needed a float64 case. Typed results through WithEventDataDecoder are in the stacked follow-up, not here.
  • Departs from the proposed answer to durable: defects found in the platform-redesign review #214 question 4 ("Recover now, redispatch later"): the Handle stops, and Recover fails while the handler is down. A handler that always refuses an effect wedges the instance. This needs a ruling.
  • journalFormat is now 2, which gates the re-send rule above. Main's format check is unchanged.

Out of scope, tracked separately:

Why

Effects from entry, timer and service transitions never reached the handler, and a failed effect could be compacted away and never retried.

Relates to: #214

Evidence

On GOTOOLCHAIN=go1.27.4, with -race:

Command Result
go test ./... in every module Pass
golangci-lint v2.15.0 in durable and state 0 issues

Each key guarantee was mutation-checked: reverting Tick's dispatch, Recover's re-send, the old-version gate, the Store refusal, the Tick driver absorb (live and replay) or the panic guards makes a test fail.

Checklist

  • Commits are signed off (git commit -s) per the DCO
  • Conventional commit messages (type: subject)
  • mage check passes locally (lint + race tests + govulncheck)
  • Tests added/updated for the change
  • Public API changes documented (godoc) and, if applicable, noted for the changelog

@reka18 reka18 added not-ready On every new PR until all review dimensions are confirmed wait:correctness ACR pending: correctness wait:regression-safety ACR pending: regression-safety wait:concurrency-discipline ACR pending: concurrency-discipline wait:replay-determinism ACR pending: replay-determinism wait:api-compatibility ACR pending: api-compatibility wait:missed-context ACR pending: missed-context wait:human-readability ACR pending: human-readability labels Nov 5, 2026
@reka18
reka18 force-pushed the fix/durable-effect-dispatch branch from ebb5863 to ef9c0a8 Compare November 5, 2026 16:38
@reka18
reka18 force-pushed the fix/durable-effect-dispatch branch from ef9c0a8 to d46adf2 Compare November 5, 2026 16:40
@reka18
reka18 force-pushed the fix/durable-effect-dispatch branch from d46adf2 to cc4d8cc Compare November 5, 2026 16:53
@reka18
reka18 force-pushed the fix/durable-effect-dispatch branch from cc4d8cc to d657891 Compare November 5, 2026 17:09
@reka18
reka18 force-pushed the fix/durable-effect-dispatch branch from d657891 to 3e41947 Compare November 5, 2026 17:14
@reka18
reka18 force-pushed the fix/durable-effect-dispatch branch from 3e41947 to 3edd2ee Compare November 5, 2026 17:22
@reka18
reka18 force-pushed the fix/durable-effect-dispatch branch from 3edd2ee to aae543d Compare November 5, 2026 17:33
@reka18
reka18 added this pull request to stack #238 November 5, 2026 17:34
@reka18
reka18 force-pushed the fix/durable-effect-dispatch branch from aae543d to 50bbc98 Compare November 5, 2026 17:42
@reka18
reka18 force-pushed the fix/durable-effect-dispatch branch from 50bbc98 to a14c4ed Compare November 5, 2026 18:04
@reka18
reka18 force-pushed the fix/durable-effect-dispatch branch from a14c4ed to 90bcbfb Compare November 5, 2026 18:20
Only Fire dispatched domain effects. Tick and RunService now record a step
through the same helper as Fire, then dispatch its effects as Fire does, after
the append and before any checkpoint. A failed dispatch fails the Handle
before its checkpoint, so a later checkpoint can't compact the effect away,
and Recover retries it. Start applies the initial entry's effects before the
baseline, so a handler failure leaves no instance; they're at-least-once.
DeliverToActor records through the same helpers, and persistStep is gone.

- EffectInstance gives a handler its instance id, because effect ids repeat
  across instances.
- state.ServiceRunner gains WithResultTransform, so a service's result
  reaches the machine in the JSON-decoded form a replay supplies, and an
  error message-only. A transform that returns no error for an unbound
  service doesn't turn it into a success. A result whose JSON form won't
  decode fails RunService and records nothing.
- One helper calls the effect handler for Start, a step and Recover, and
  turns a handler panic into ErrEffectDispatch; the shared dispatch path does
  the same for a DispatchStore whose Dispatched or MarkDispatched panics or
  fails. A service error whose Error method panics settles with a message
  saying so, its invalid UTF-8 replaced.
- A KindedEffect that won't marshal, or whose MarshalJSON panics, is recorded
  by id and kind, with the reason in Meta under crucible.marshalError, on
  every path including Fire, and dispatches as any effect does.
- Recover re-sends a tick's and a service settle's effects. journalFormat is
  2, and Recover sends them only for a Record of format 2 or later, so it
  doesn't send an older version's, which never dispatched them.
- An effect handler wired with a Store that isn't a DispatchStore is refused
  by Start, Recover and the stateless Runner.Fire, and by dispatch itself,
  with the new ErrNoDispatchStore, a permanent error that doesn't match
  ErrEffectDispatch. It used to skip every effect silently.
- Tick hands each fired timer transition's effects to the service runner and
  then the actor system, under the step's context, live and on replay for a
  tick of format 2 or later, so a timer that leaves a state stops its service
  and actor, and one that enters a state starts and spawns them. This covers
  #236's Tick items and unskips its two tests.
- A settle leaves its follow-on services to the ServiceRunner, which already
  armed them, as replay does, and a checkpoint written without a service
  registry keeps the settled services.

Effects from a parent transition an actor drives are still not dispatched
(#257).

Relates to #214.

Signed-off-by: Ray Karnes <ray.karnes@stablekernel.com>
Tick checked ctx before handing the fired timers' effects to the service
runner and the actor system, then only looked for a parent Fire a context
refused. A ctx that ended while the tick spawned its actors could cost a
message one actor sent another, which no Fire reports, so the tick was
journaled and Recover delivered that message under a live context, which
live never applied. Tick now rechecks ctx after the absorb, as RunService
does after its settle: it fails the Handle and records nothing, and a
recovered Handle re-arms the timer.

Tests, each committed either way:

- Tick: a context that ends while the tick spawns actors records nothing.
- Fire, Tick and RunService dispatch a recorded step's effects under the
  caller's context alike: a handler that ignores an ended context applies
  the effect and the step succeeds; one that honors it fails the Handle
  with ErrEffectDispatch, and Recover sends the effect once.
- RunService: a result whose JSON form won't decode, or that has no JSON
  form, settles onDone before the step fails. Its Assign runs on the
  stand-in, and the state it enters spawns its actor, then RunService
  returns ErrHandleFailed and records nothing. A reducer that panics on the
  stand-in refuses the Fire, and RunService still returns ErrHandleFailed.
  This pins today's behavior for a design decision.

Signed-off-by: Ray Karnes <ray.karnes@stablekernel.com>
Fire looked only for a parent Fire a context refused after handing the
event's effects to the actor system. A ctx that ended while the Fire
spawned its actors could cost a message one actor sent another, which no
Fire reports, so the Fire was journaled and Recover delivered that
message under a live context, which live never applied. A Fire that
spawned an actor now rechecks ctx after the absorb, as Tick and
RunService do: it fails the Handle and records nothing, and the event
fires again on a recovered Handle. A Fire that spawned no actor still
records when its context ends once the kernel applied the event.

DeliverToActor already rechecks ctx after the delivery. Start and
Recover keep their documented rule: a context that ends once their
actors spawned, with no parent Fire refused, doesn't fail them.

Document what RunService does with a result whose JSON form won't
decode, or that has no JSON form: the onDone Assign runs once on a
stand-in and the onDone state starts its actors and services before
RunService returns ErrHandleFailed and records nothing. Keep results
JSON round-trippable, and have an onDone Assign check the payload's
type. A stacked change, #258, refuses such a result before any Assign
runs.

Tests:

- Fire: a context that ends while the Fire spawns actors records
  nothing, and the event fires again on the recovered Handle.
- Fire: without an actor palette, a context that ends once the kernel
  applied the event still records.

Signed-off-by: Ray Karnes <ray.karnes@stablekernel.com>
Fire rechecked an ended ctx after its absorb only when it spawned an
actor, and Tick whenever a timer fired. Neither matched what an ended ctx
can cost. A Fire that sent a running actor a message (SendTo, ForwardTo)
recorded though the actor refused the message under the ended ctx, so
the message was lost for good: the caller saw success, and a recovered
Handle starts the actor fresh. A Tick on a machine with timers and
services but no actor palette failed its Handle when ctx ended during
the absorb, where the same Fire records, though handing services to the
runner runs nothing ctx can refuse.

Fire and Tick now share one rule: recheck ctx after the absorb when the
step handed a wired actor system a spawn or a message to an actor.
Stopping an actor, a response with no sender, and timers and services
don't count, so a Fire that touched no actor still records when ctx ends
after the apply. Tick still fails when ctx ends while its timers fire.
RunService keeps rechecking ctx whatever its absorb did: the service
itself ran under ctx, so its outcome can be one ctx's ending caused.

The RunService doc no longer names a pending change.

Tests, each committed either way:

- Fire (by SendTo and by ForwardTo) and Tick: a ctx that ends while the
  step messages a running actor records nothing, and the message is
  delivered when the step runs again on a recovered Handle.
- Fire and Tick: with timers and a service but no actor palette, a ctx
  that ends after the kernel applied the step records, and the service
  is in flight live and recovered.
- Fire: a ctx that ends while the step stops an actor and responds to
  no sender records.
- Recover: an older version's journal, of format 0 or 1, whose tick
  left a state with its service in flight, recovers with the service
  in flight; a checkpoint then drops it, a settle follows, and every
  later Recover fails with "no in-flight service to settle". This pins
  today's behavior for a decision.

Signed-off-by: Ray Karnes <ray.karnes@stablekernel.com>
…il only on refused parent Fires

A parent's forwardTo forwarded nothing: Fire and Tick handed the actor
system their effects without the event. Fire now absorbs through
AbsorbFor with its event, and Tick with each timer's event, rebuilt
from the trace with the Runner's event codec. Actor deliveries aren't
replayed, so Recover starts the actor fresh, as for a SendTo. This was
already so on main for Fire.

A step could spawn an actor for a state a parent Fire its absorb drove
had already left, as when one region's actor exits another region's
state, by one Fire or by timers that fire in one Tick. The live Handle
ran an orphan Recover never spawns. The step now feeds the actor system
one effect at a time and skips a state's spawn whose actor a parent
Fire since stopped.

Fire and Tick no longer fail the Handle because their context ended
after they handed actors work; only a parent Fire a context refused
fails them, as for Start and Recover. A message an actor refuses under
the ended context is lost, as actor deliveries are across Recover, and
failing the step would have the retry run actor behaviors again.

Signed-off-by: Ray Karnes <ray.karnes@stablekernel.com>
…mers; decode a timer's event only to forward it

c393fd3 skipped a step's spawn for a state a parent Fire had left, but
only in Fire and Tick. Start and Recover still spawned the
configuration's actors in one batch, so when one region's actor sent
the parent a message as it started that left another region's state,
that state's actor spawned after, and ran where a Recover from the
journal never spawns it. Start and Recover's new actors now go through
the same per-effect absorb. Recover respawns the actors that were
running in one batch, so when one's respawn fails with an onError that
leaves another of their states, it now stops each respawned actor whose
state isn't active.

Within a Tick, a timer that entered a state spawned its actor even when
a later timer of the same tick left it, so the actor's behavior ran
before the later timer stopped it. A step now skips a state's spawn that
a later effect of the step stops, the later timers' included.

Checking the declaring state against the configuration instead wasn't
enough on its own: state exposes active leaves only, and a state left
and entered again in one step is active at the end, so its first spawn
would still run.

Tick decoded every fired timer's event through the event codec and
dropped a decode error. It now decodes only for a forwardTo an actor
system can carry. On a decode error it forwards nothing, records the
tick, dispatches its effects, and returns the codec's error with the
results; the Handle carries on, as when an actor can't take a message.
A step with no event to forward no longer hands the actor system a
ForwardEvent, which it delivered as a nil event.

Signed-off-by: Ray Karnes <ray.karnes@stablekernel.com>
…nil event

Signed-off-by: Ray Karnes <ray.karnes@stablekernel.com>
joshua-temple
joshua-temple previously approved these changes Nov 17, 2026

@joshua-temple joshua-temple left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Accepted behavior change: ErrNoDispatchStore. Stateless Runner.Fire on a custom Store now errors instead of silently skipping effects.
  • Please add a changelog note for it.
  • Follow-ups filed: #263, #264
  • Non-blocking suggestions
    • Export a sentinel for the non-fatal Tick error (runner.go ~1133)
    • Route recordEffects marshal failures to the observer (effect.go ~119)
    • Restore rec.Step in the panic message (effect.go ~239)
    • Run full mage check before merge

@reka18

reka18 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Punting two of the non-blocking suggestions, since both add public API:

The changelog note, rec.Step in the panic message and the full mage check are coming in this PR.

A panic in the Store's Dispatched or MarkDispatched now reports the step it
dispatched for, as it did before. The changelog now says a stateless
Runner.Fire on a Store without dispatch tracking returns ErrNoDispatchStore.

Signed-off-by: Ray Karnes <ray.karnes@stablekernel.com>
@reka18

reka18 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Done in 2176d0e:

  • The changelog now says a stateless Runner.Fire on a Store without DispatchStore used to succeed with its effects skipped, and now returns ErrNoDispatchStore.
  • A panic in the Store's Dispatched or MarkDispatched names the step again ("step N for ..."), with a test.
  • mage check passes locally: lint 0 issues, race tests, and no vulnerabilities in all 13 modules.

@reka18

reka18 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@reka18

reka18 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up: #255, the service-result cleanups from this PR's reviews, is addressed in #273. It caches each type's full spelling. The settledJSON holder moved to #271 for a ruling, and fullTypeName keeps its full spelling so the replay type check stays strict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

confirmed:api-compatibility ACR clean: api-compatibility confirmed:concurrency-discipline ACR clean: concurrency-discipline confirmed:correctness ACR clean: correctness confirmed:human-readability ACR clean: human-readability confirmed:missed-context ACR clean: missed-context confirmed:regression-safety ACR clean: regression-safety confirmed:replay-determinism ACR clean: replay-determinism ready All ACR dimensions confirmed - clear to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants